package carTest.create;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: 张德长 dechang.zhang@ucarinc.com
 * @date: 10:48:53 2018/12/13
 * @version:2.0
 */
public class User {
    public static JSON login(HttpClient client, String userName, String pass) {
        JSON userInfo = null;
        String url = "http://cartest.10101111.com/usermanage/login.do";
        List<NameValuePair> list = new ArrayList<NameValuePair>();
        list.add(new BasicNameValuePair("name", userName));
        list.add(new BasicNameValuePair("password", pass));
        HttpPost httpPost = new HttpPost(url);
        UrlEncodedFormEntity uefEntity = null;
        try {
            uefEntity = new UrlEncodedFormEntity(list, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            System.out.println("登录封装参数出错");
            return userInfo;
        }
        httpPost.setEntity(uefEntity);
        HttpResponse response = null;
        try {
            response = client.execute(httpPost);
            HttpEntity entity = response.getEntity();
            String web = EntityUtils.toString(entity, "UTF-8");
            JSON loginResponse = JSON.parseObject(web);
            if ("success".equals(((JSONObject) loginResponse).getString("result"))) {
            } else {
                System.out.println(url + "登录时出错");
                return userInfo;
            }
            // System.out.println( web);
        } catch (IOException e) {
            System.out.println(url + "登录响应时出错" + e);
        }
        url = "http://cartest.10101111.com/usermanage/findUserId?userName=" + userName;
        HttpGet httpGet = new HttpGet(url);
        //httpGet.setConfig(requestConfig);
        //执行 get请求
        response = null;
        try {
            response = client.execute(httpGet);
            //返回获取实体
            HttpEntity entity = response.getEntity();
            //获取网页内容，指定编码
            String web = EntityUtils.toString(entity, "UTF-8");
            userInfo = JSON.parseObject(web);
        } catch (IOException e) {
            System.out.println(url + "获取登录人员信息响应内容出错" + e);
            return userInfo;
        }
        return userInfo;
    }
}
